﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using SchoolPointe.Menus.Models;
using SchoolPointe.Menus.ViewModels;

namespace SchoolPointe.Menus.Drivers
{
    public class BetterMenuDriver : ContentPartDriver<BetterMenuPart>
    {
        //GET
        protected override DriverResult Editor(BetterMenuPart part, dynamic shapeHelper)
        {
            var vm = new MenuViewModel(part);

            return ContentShape("Parts_BetterMenu_Edit",
                () => shapeHelper.EditorTemplate(
                    TemplateName: "Parts/BetterMenu",
                    Model: vm,
                    Prefix: Prefix));
        }
        //POST
        protected override DriverResult Editor(BetterMenuPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            var vm = new MenuViewModel(part);
            if (updater.TryUpdateModel(vm, Prefix, null, null))
            {
                part.Name = vm.Name;
            }
            return Editor(part, shapeHelper);
        }

        protected override void Importing(BetterMenuPart part, ImportContentContext context)
        {
            part.Name = context.Attribute(part.PartDefinition.Name, "Name");
        }

        protected override void Exporting(BetterMenuPart part, ExportContentContext context)
        {
            context.Element(part.PartDefinition.Name).SetAttributeValue("Name", part.Name);
        }
    }
}